#include "playscene.h"
#include<QDebug>
#include<QMenu>
#include<QMenuBar>
#include<QAction>
#include"mypushbutton.h"
#include<QPainter>
#include<QTimer>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
#include<QPropertyAnimation>
#include<QSoundEffect>

PlayScene::PlayScene(int levelNum){
    QString str=QString("进入了第%1关").arg(levelNum);
    qDebug()<<str;
    this->levelIndex=levelNum;

    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("翻金币场景");

    //创建菜单栏
    QMenuBar *bar=menuBar();
    setMenuBar(bar);

    QMenu* startMenu=bar->addMenu("开始");
    QAction* quitAction= startMenu->addAction("退出");

    connect(quitAction,&QAction::triggered,[=](){
        this->close();
    });

    QSoundEffect *backSound=new QSoundEffect(this);
    backSound->setSource(QUrl::fromLocalFile(":/res/BackButtonSound.wav"));
    QSoundEffect *flipSound=new QSoundEffect(this);
    flipSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));
    QSoundEffect *winSound=new QSoundEffect(this);
    winSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));

    MyPushButton* backBtn=new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height()-20);

    connect(backBtn,&MyPushButton::clicked,[=](){
        backSound->play();
        QTimer::singleShot(500,this,[=](){
            emit this->chooseSceneBack();
        });
    });

    QLabel* label=new QLabel();
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    QString level_str=QString("Level:%1").arg(this->levelIndex);
    label->setFont(font);
    label->setText(level_str);
    label->setGeometry(30,this->height()-60,120,50);

    dataConfig dataconfig;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            this->gameArray[i][j]=dataconfig.mData[levelNum][i][j];
        }
    }
    QLabel* winLabel=new QLabel();
    QPixmap tempPix(":/res/LevelCompletedDialogBg.png");
    //只有设置为Geometry才可以用动画代码
    winLabel->setGeometry(0,0,tempPix.width(),tempPix.height());
    winLabel->setFixedSize(tempPix.width(),tempPix.height());
    winLabel->setPixmap(tempPix);
    winLabel->setParent(this);
    winLabel->move((this->width()-tempPix.width())*0.5,-tempPix.height());

    //显示金币背景图案
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            QPixmap pix;
            pix.load(":/res/BoardNode.png");
            QLabel* label=new QLabel();
            label->setFixedSize(pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(57+i*50,200+j*50);
            QString coinPath;
            if(this->gameArray[i][j]){
                coinPath=":/res/Coin0001.png";
            }
            else{
                coinPath=":/res/Coin0008.png";
            }
            MyCoin* coin=new MyCoin(coinPath);
            coin->setParent(this);
            coin->move(59+i*50,204+j*50);
            //qDebug()<<QString::number(i*4+j);
            coin->posX=i;
            coin->posY=j;
            coin->flag=this->gameArray[i][j];

            coinBtn[i][j]=coin;

            connect(coin,&MyCoin::clicked,[=](){
                flipSound->play();
                for(int i=0;i<4;i++){
                    for(int j=0;j<4;j++){
                        this->coinBtn[i][j]->isWin=true;
                    }
                }

               coin->changeFlag();
               this->gameArray[i][j]=this->gameArray[i][j]==0?1:0;
               QTimer::singleShot(300,this,[=](){
                   if(coin->posX+1<4){
                       coinBtn[coin->posX+1][coin->posY]->changeFlag();
                       this->gameArray[coin->posX+1][coin->posY]=this->gameArray[coin->posX+1][coin->posY]==0?1:0;
                   }
                   if(coin->posX-1>-1){
                       coinBtn[coin->posX-1][coin->posY]->changeFlag();
                       this->gameArray[coin->posX-1][coin->posY]=this->gameArray[coin->posX-1][coin->posY]==0?1:0;
                   }
                   if(coin->posY+1<4){
                       coinBtn[coin->posX][coin->posY+1]->changeFlag();
                       this->gameArray[coin->posX][coin->posY+1]=this->gameArray[coin->posX][coin->posY+1]==0?1:0;
                   }
                   if(coin->posY-1>-1){
                       coinBtn[coin->posX][coin->posY-1]->changeFlag();
                       this->gameArray[coin->posX][coin->posY-1]=this->gameArray[coin->posX][coin->posY-1]==0?1:0;
                   }
                   for(int i=0;i<4;i++){
                       for(int j=0;j<4;j++){
                           this->coinBtn[i][j]->isWin=false;
                       }
                   }

                   bool iswin=isWin();
                   if(iswin){
                       winSound->play();
                       //qDebug()<<"胜利了";
                       for(int i=0;i<4;i++){
                           for(int j=0;j<4;j++){
                               coinBtn[i][j]->isWin=true;
                           }
                       }
                       QPropertyAnimation* animation=new QPropertyAnimation(winLabel,"geometry");
                       animation->setDuration(300);
                       animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));
                       animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                       animation->setEasingCurve(QEasingCurve::OutBounce);
                       animation->start();
                   }
               });
            });
        }
    }
}

void PlayScene::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    pix.load(":/res/Title.png");
    pix=pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);
}
bool PlayScene::isWin(){
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            if(!gameArray[i][j]){
                return false;
            }
        }
    }
    return true;
}
